import { api } from '../api';
// 获取基本个人资料
export const getUserMeta = async () => {
  try {
    const response = await api.get("/java/user/get_usermeta");
    return response.data;
  }
  catch (error) {
    throw new Error("用户信息请求失败");
  }
};
// 更新用户基本资料
export const updateUserMeta = async (userMeta: {
  nickName: string;
  sex: string;
  userIntroduction: string;
  realName: string;
  school: string;
  college: string;
  major: string;
  studentId: string;
  phoneNumber: string;
  email: string;
  participationType: string;
  campus: string;
  studentStatus: string;
  enrollmentYear: string;
  projectStatus: string;
  projectType: string;
  projectName: string;
}) => {
  try {
    const response = await api.put("/java/user/update_usermeta", userMeta);
    return response.data;
  }
  catch (error) {
    console.error("更新用户基本资料失败", error);
    throw error;
  }
};

// 更新用户图片
export const updateUserImage = async (userImg: { userImage?: File; userCover?: File }) => {
  try {
    // 创建 FormData 实例
    const formData = new FormData();

    // 根据条件将文件添加到 FormData 中，仅添加存在的文件
    if (userImg.userImage)
      formData.append("userImage", userImg.userImage);
    if (userImg.userCover)
      formData.append("userCover", userImg.userCover);

    // 发起 PUT 请求，并传递 FormData
    const response = await api.put("/java/user/update_user_image", formData, {
      headers: {
        // 使用 multipart/form-data 以便传输文件
        "Content-Type": "multipart/form-data",
      },
    });

    return response.data;
  }
  catch (error) {
    console.error("更新用户图片失败", error);
    throw error;
  }
};
